/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.common.utils;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.SessionIdGenerator;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * 封装各种生成唯一性ID算法的工具类.
 *
 * @author ThinkGem
 * @version 2013-01-15
 */
@Service
@Lazy(false)
public class IdGen implements SessionIdGenerator{
    /**
     * 封装JDK自带的UUID, 通过Random数字生成, 中间无-分割.
     */
    public static String uuid(){
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public final static int BAR_CODE_LENGTH = 8;

    public static int formatCount = 0;
    public static String prefixCount = null;

    public static String getMaxBarCode(String temMaxBarCode){
        String maxBarCode;
        int length = temMaxBarCode.length();
        int barCodeLength = BAR_CODE_LENGTH;

        //  取temMaxBarCode最后的数字
        char[] chars = temMaxBarCode.toCharArray();

        int end = (chars.length >= barCodeLength) ? barCodeLength : 0;

        for(int i = chars.length - 1; i >= end; i--){
            char item = chars[i];
            if(item < 48 || item > 57){
                barCodeLength = chars.length - i - 1;
                break;
            }
        }

        if(length > barCodeLength){
            maxBarCode = temMaxBarCode.substring(length - barCodeLength, length);
            IdGen.prefixCount = temMaxBarCode.substring(0, length - barCodeLength);
        }
        else{
            maxBarCode = temMaxBarCode;
            IdGen.prefixCount = "";
        }
        return maxBarCode;
    }

    @Override
    public Serializable generateId(Session session){
        return IdGen.uuid();
    }

    /**
     * 单例模式生成唯一编号
     *
     * @return 唯一编号
     */
    public static synchronized String getMaxCode(int barCodeLength){
        // 生成前缀
        formatCount++;
        // 格式化数字 生成 0000000001格式
        String pattern = "";
        for(int i = 0; i < barCodeLength; i++){
            pattern += "0";
        }

        DecimalFormat df = new DecimalFormat(pattern);
        return prefixCount + df.format(formatCount);
    }

    private static int generateCount = 0;
    private static final int MAX_GENERATE_COUNT = 999;

    /**
     * 单例模式生成唯一编号
     *
     * @return 唯一编号
     */
    public static synchronized String getUniqueString(){
        if(generateCount > MAX_GENERATE_COUNT){
            generateCount = 0;
        }
        // 生成时间串
        String dateStr = new SimpleDateFormat("yyMMddHHmmss").format(new Date());
        // 格式化数字 生成 00001格式
        DecimalFormat df = new DecimalFormat("00");
        String formatCount = df.format(generateCount);

        generateCount++;

        return dateStr + formatCount;
    }
}
